import util from "../../utils/util";

const app = getApp()

Page({
    data: {
        sHeight: null,
        sWidth: null,
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
    },
    onLoad: function (options) {
        console.log('页面')
        if (app.globalData.userInfo) {
            console.log("已有用户信息")
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            app.userInfoReadyCallback = res => {
                // 所以此处加入 callback 以防止这种情况
                console.log("2")
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({

                success: res => {
                    console.log("3")
                    app.globalData.userInfo = res.userInfo
                    app.postUserInfo(res,app)

                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
    },
    onShow() {
        // this.getOpenId()
        this.setData({
            sHeight: app.globalData.screenH,
            sWidth: app.globalData.screenW
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    navToSimple() {
        wx.navigateTo({
            url: "/pages/score/score"
        })
    },
    navToComplex() {
        wx.showToast({
            title: '待开放',
            icon: 'success',
            image: '/src/images/404.png',
            duration: 2000
        })
    },
    getUserInfo: function (e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        app.postUserInfo(e.detail,app)
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    getOpenId() {
        let stop = true;
        if (!app.globalData.code) {
            console.log('没有code')
            wx.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    app.globalData.code = res.code
                    stop = false
                }
            })
        } else {
            console.log('存在code')
            stop = false
        }
        while(stop){
            console.log('wait')
            setTimeout(function(){  }, 300);
        }
        wx.request({
            url: util.baseUrl + '/wx/user/' + util.appid + '/login',
            data: {
                js_code: app.globalData.code
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'GET',
            success: function (res) {
                // console.log('sessionKey ' + res.data.session_key)
                app.globalData.sessionKey = res.data.session_key
                app.globalData.openid = res.data.openid
                console.log('获取到了openid:' + res.data.openid)
            }
        })
    },
    copy:function(e){
        let url = e.currentTarget.dataset.url
        wx.setClipboardData({
            data: url, //这个是要复制的数据
            success (res) {
                wx.getClipboardData({
                    success (res) {
                        console.log(res.data) // data
                    }
                })
            }
        })
    }
});
